package com.zsc.wxm.ysh.anjo.house.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.wxm.ysh.anjo.house.cache.HouseCollectionCacheService;
import com.zsc.wxm.ysh.anjo.house.cache.ResourcesCacheService;
import com.zsc.wxm.ysh.anjo.house.mapper.HouseCollectionMapper;
import com.zsc.wxm.ysh.anjo.house.model.HouseCollection;
import com.zsc.wxm.ysh.anjo.house.model.Resources;
import com.zsc.wxm.ysh.anjo.house.service.HouseCollectionService;
import com.zsc.wxm.ysh.anjo.service.base.model.es.vo.EsDataVO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.dto.HouseCollectionDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.vo.HouseCollectionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/3/23 23:08
 */
@Slf4j
@Service
public class HouseCollectionServiceImpl extends ServiceImpl<HouseCollectionMapper, HouseCollection> implements HouseCollectionService {

    @Resource
    private HouseCollectionCacheService houseCollectionCacheService;

    @Resource
    private ResourcesCacheService resourcesCacheService;

    @Resource
    private ResourcesServiceImpl resourcesService;

    @Override
    public void add(HouseCollectionDTO param) {
        //前置校验
        LambdaQueryWrapper<HouseCollection> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(HouseCollection::getHouseId, param.getHouseId());
        wrapper.eq(HouseCollection::getUserId, param.getUserId());
        List<HouseCollection> list = this.list(wrapper);
        if (CollUtil.isNotEmpty(list)) {
            return;
        }
        HouseCollection houseCollection = new HouseCollection();
        BeanUtils.copyProperties(param, houseCollection);
        this.save(houseCollection);
    }

    @Override
    public List<HouseCollectionVO> listByUserId(Long userId) {
        List<HouseCollection> list = houseCollectionCacheService.getByUserIdCache(userId);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<Long> houseIds = list.stream().map(HouseCollection::getHouseId).collect(Collectors.toList());
        List<Resources> resourcesList = new ArrayList<>();
        houseIds.forEach(id -> resourcesList.add(resourcesCacheService.getByIdCache(id)));
        List<HouseCollectionVO> vos = new ArrayList<>();
        for (Resources resources : resourcesList) {
            HouseCollectionVO vo = new HouseCollectionVO();
            EsDataVO esDataVo = resourcesService.initEsDataVO(resources);
            BeanUtils.copyProperties(esDataVo, vo);
            vos.add(vo);
        }
        return vos;
    }
}
